//
//  BaseNavigationController.m
//  GK
//
//  Created by Seven on 2019/1/15.
//  Copyright © 2019 Seven. All rights reserved.
//

#import "BaseNavigationController.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@implementation UINavigationController(pop)

- (UIViewController *)customPopViewControllerAnimated:(BOOL)animated{
    NSInteger count = self.viewControllers.count;
    UIViewController *vc = [self.viewControllers objectAtIndex:MAX(0, count-2)];
    [self popToViewController:vc animated:animated];
    return vc;
}

@end

@interface BaseNavigationController ()

@end

@implementation BaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage *backImage = [UIImage imageNamed:@"Default_back"];
    [self.navigationBar setBackIndicatorImage:backImage];
    [self.navigationBar setBackIndicatorTransitionMaskImage:backImage];
    // Do any additional setup after loading the view.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated{
    [super setNavigationBarHidden:hidden animated:animated];
}

/*
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //viewController.hidesBottomBarWhenPushed = YES;
    [super pushViewController:viewController animated:animated];
    custom_dispatch_after(animated ? 0.3 : 0, ^{
        if (self.viewControllers.lastObject != viewController) {
            NSMutableArray *arrView = [NSMutableArray arrayWithArray:self.viewControllers];
            [arrView addObject:viewController];
            [self setViewControllers:arrView animated:NO];
        }
    });
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
    if (self.topViewController.fd_prefersNavigationBarHidden) {
        int count = self.viewControllers.count;
        UIViewController *vc = [self.viewControllers objectAtIndex:MAX(0, count-2)];
        //[self.topViewController wr_setNavBarBarTintColor:vc.wr_navBarBarTintColor];
    }
    return [super popViewControllerAnimated:animated];
}
//*/

- (BOOL)shouldAutorotate{
    if (self.presentedViewController) {
        return self.presentedViewController.shouldAutorotate;
    }
    if (self.topViewController) {
        return self.topViewController.shouldAutorotate;
    }
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    if (ITOrientationManager.sharedInsatnce.supportLandspcape) {
        return UIInterfaceOrientationMaskLandscape;
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
    if (self.presentedViewController) {
        return self.presentedViewController.supportedInterfaceOrientations;
    }
    if (self.topViewController) {
        return self.topViewController.supportedInterfaceOrientations;
    }
    return UIInterfaceOrientationMaskPortrait;
}

@end


@implementation UIAlertController (supportedInterfaceOrientations)

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
#endif

- (BOOL)prefersHomeIndicatorAutoHidden {
    return YES;
}

@end
